package com.yourname.www.frame.dialog;

import com.yourname.www.dao.JdbcHelper;
import com.yourname.www.entity.Student;
import com.yourname.www.model.StudentModel;
import com.yourname.www.util.Tools;
import com.yourname.www.util.WindowUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.plaf.DimensionUIResource;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.*;
import java.util.List;

public class QueryStudentInfoDialog extends JDialog {
    private final JTable studentTable;
    private final JScrollPane jsp;
    private final JTextField keywordText;
    private final JComboBox<String> searchType;
    private final JButton queryBtn;
    private final JButton queryMoreBtn;
    private final JButton detailBtn;
    private JDialog jd;
    //1
    private JPanel jp1,jp2,jp3;	//面板。组件的容器
    private JLabel query_Label;
    private JdbcHelper jdbcHelper = new JdbcHelper();
    private Container c;
    public QueryStudentInfoDialog(JFrame jf){
        super(jf,true);
        setBasicInfo();
        this.c = this.getContentPane();
        jp1 = new JPanel();
        //2
        query_Label = new JLabel("请输入查询信息:");
        keywordText = new JTextField(15);
        searchType = new JComboBox<String>(new String[]{
                "全部","学号","姓名","性别","班级","年级","专业","院系"
        });
        queryBtn = new JButton("查询");
        queryMoreBtn = new JButton("多条件查询");

        jp1.add(query_Label);
        jp1.add(keywordText);
        jp1.add(searchType);
        jp1.add(queryBtn);
        jp1.add(queryMoreBtn);
        //3 设置面板的位置【把面板添加到容器】
        //BorderLayout.NORTH 顶部
        //BorderLayout.SOUTH 底部
        //BorderLayout.WEST  左边
        //BorderLayout.EAST  左边
        //BorderLayout.CENTER  中间
        c.add(jp1,BorderLayout.NORTH);	//添加面板
        //实例化第二个面板【子容器】
        jp2 = new JPanel();
        //创建表格
        studentTable = new JTable();
        studentTable.setModel(new StudentModel(new ArrayList<>()));
        //设置表格可按列排序
        studentTable.setAutoCreateRowSorter(true);
        //设置表头居中
        DefaultTableCellRenderer defaultRenderer = (DefaultTableCellRenderer) studentTable.getTableHeader().getDefaultRenderer();
        defaultRenderer.setHorizontalAlignment(JLabel.CENTER);
        //设置表数据居中
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(JLabel.CENTER);
        studentTable.setDefaultRenderer(Object.class,renderer);
        //自适应表格宽度
        Tools.fitTableColWidth(studentTable);
        //把表格添加到可滚动的容器中
        jsp = new JScrollPane(studentTable);
        //设置JScrollPane的宽度
        jsp.setPreferredSize(new Dimension(740,400));
        jp2.add(jsp,BorderLayout.CENTER);
        //把可滚动的容器添加到主容器
        c.add(jp2,BorderLayout.CENTER);
        jp3 = new JPanel();
        detailBtn = new JButton("详细信息");
        jp3.add(detailBtn);
        c.add(jp3,BorderLayout.SOUTH);
        //查询按钮单击事件
        queryBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取查询条件
                Map<String,String> where = new HashMap<>();
                String key = keywordText.getText().trim();
                if(key.length()>0){
                    where.put(getColumnName(),key);
                }
                List<Student> studentList = jdbcHelper.queryStudentList(where);
                //重置表格数据
                studentTable.setModel(new StudentModel(studentList));
                //自适应表格宽度
                Tools.fitTableColWidth(studentTable);
            }
            private String getColumnName(){
                String columnName = "";
                switch (searchType.getSelectedItem().toString()){
                    case "全部":
                        columnName= "all";break;
                    case "姓名":
                        columnName= "Student_Name";break;
                    case "姓别":
                        columnName= "Student_Sex";break;
                    case "学号":
                        columnName= "Student_Id";break;
                    case "班级":
                        columnName= "Classe";break;
                    case "年级":
                        columnName= "Grade";break;
                    case "专业":
                        columnName= "Major_Name";break;
                    case "院系":
                        columnName= "Depart_Name";break;
                }
                return columnName;
            }
        });
        detailBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("选中了哪一行："+studentTable.getSelectedRow());
                System.out.println(studentTable.getModel().getValueAt(studentTable.getSelectedRow(), 0));
            }
        });
        queryMoreBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new QueryMoreDialog(jd,jdbcHelper,studentTable);
            }
        });
        this.setVisible(true);
    }

    //设置基本信息
    private void setBasicInfo() {
        this.jd = this;
        this.setTitle("查询学生信息");
        //设置Logo
        try{
            Image image = ImageIO.read(this.getClass().getResource("/logo.png"));
            this.setIconImage(image);
        }catch (IOException e) {
            e.printStackTrace();
        }
        //设置大小
        this.setSize(800,550);
        //设置是否可以调节大小
        this.setResizable(false);
        //设置关闭行为【其实默认就是，可以不设置，了解方法即可】
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        //设置居中
        WindowUtil.setFrameCenter(this);
    }
}
